Days of the Condor

Projects About

Programmatically focus content inside an iFrame

Oct 5, 2011
OCT
5
2011

This is a short one. On the Pixie landing page I was trying to use jQuery to focus on a game embedded via an iFrame. I tried

$ ->
  $('iframe').contents().find('canvas').focus()

but that didn’t work.

It turns out to get this working you need setTimeout. I guess the browser isn’t done loading the iFrame contents by the time jQuery says the DOM is ready.

$ ->
  setTimeout ->
    $('iframe')[0].contentWindow.focus()
  , 100

Here’s the related Stack Overflow question